<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coze 工作流文件分析</title>
    <style>
        :root {
            --primary-color: #3b82f6;
            --primary-hover-color: #2563eb;
            --background-color: #f8f9fa;
            --card-background-color: #ffffff;
            --text-color: #212529;
            --muted-text-color: #6c757d;
            --border-color: #dee2e6;
            --error-color: #dc3545;
            --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--background-color);
            color: var(--text-color);
            margin: 0;
            padding: 2rem;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            min-height: 100vh;
        }

        .container {
            width: 100%;
            max-width: 800px;
            background-color: var(--card-background-color);
            border-radius: 12px;
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background-color: var(--primary-color);
            color: white;
            padding: 1.5rem 2rem;
            text-align: center;
        }

        .header h1 {
            margin: 0;
            font-size: 1.75rem;
            font-weight: 600;
        }

        .main-content {
            padding: 2rem;
        }

        .flash {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 6px;
            border: 1px solid transparent;
            font-size: 0.95rem;
        }

        .flash.error {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.75rem;
            font-weight: 600;
            font-size: 1rem;
        }

        .file-input-wrapper {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            box-sizing: border-box;
            font-size: 1rem;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .file-input-wrapper:focus-within {
             outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
        }

        .file-input-wrapper input[type="file"] {
            width: 100%;
            border: none;
            padding: 0;
            background: none;
        }
        .file-input-wrapper input[type="file"]:focus {
            outline: none;
        }

        #file-list {
            margin-top: 0.75rem;
            font-size: 0.9rem;
            color: var(--muted-text-color);
            padding-left: 1.25rem;
        }

        #file-list .file-item {
            margin-bottom: 0.25rem;
        }

        .submit-btn {
            width: 100%;
            padding: 0.8rem;
            font-size: 1.1rem;
            font-weight: 600;
            color: white;
            background-color: var(--primary-color);
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.2s, opacity 0.2s;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 0.5rem;
        }

        .submit-btn:hover:not(:disabled) {
            background-color: var(--primary-hover-color);
        }

        .submit-btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
        }

        .spinner {
            width: 18px;
            height: 18px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top-color: #fff;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .response-container {
            margin-top: 2rem;
            background-color: #282c34;
            color: #abb2bf;
            border-radius: 6px;
            padding: 1.5rem;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: "SF Mono", "Fira Code", "Source Code Pro", Menlo, Consolas, "Courier New", monospace;
            font-size: 0.9rem;
            min-height: 100px;
            max-height: 500px;
            overflow-y: auto;
            border: 1px solid var(--border-color);
            transition: opacity 0.3s;
        }
        .response-container.hidden {
            display: none;
        }
    </style>
</head>
<body>

<div class="container">
    <header class="header">
        <h1>Coze 工作流文件分析</h1>
    </header>

    <main class="main-content">
        <!-- 闪现消息显示区域 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="flash {{ category }}">{{ message }}</div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <form id="uploadForm" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="files">第一步：选择 .docx 文件（可多选）</label>
                <div class="file-input-wrapper">
                    <input type="file" id="files" name="files" multiple required accept=".docx">
                </div>
                <div id="file-list"></div>
            </div>
            <button type="submit" id="submitBtn" class="submit-btn">
                <span id="btn-text">第二步：开始分析</span>
            </button>
        </form>

        <div id="responseContainer" class="response-container hidden"></div>
    </main>
</div>

<script>
    const form = document.getElementById('uploadForm');
    const filesInput = document.getElementById('files');
    const fileListContainer = document.getElementById('file-list');
    const submitBtn = document.getElementById('submitBtn');
    const btnText = document.getElementById('btn-text');
    const responseContainer = document.getElementById('responseContainer');

    // 监听文件选择变化，实时显示已选文件列表
    filesInput.addEventListener('change', () => {
        fileListContainer.innerHTML = ''; // 清空旧列表
        if (filesInput.files.length > 0) {
            const list = document.createElement('ul');
            for (const file of filesInput.files) {
                const item = document.createElement('li');
                item.className = 'file-item';
                item.textContent = file.name;
                list.appendChild(item);
            }
            fileListContainer.appendChild(list);
        }
    });

    // 监听表单提交事件
    form.addEventListener('submit', async (e) => {
        e.preventDefault(); // 阻止表单默认的同步提交

        // --- 更新UI为加载状态 ---
        submitBtn.disabled = true;
        btnText.textContent = '正在处理...';
        const spinner = document.createElement('div');
        spinner.className = 'spinner';
        submitBtn.prepend(spinner);

        responseContainer.classList.remove('hidden');
        responseContainer.textContent = '正在连接服务器，请稍候...';

        try {
            const formData = new FormData(form);
            const response = await fetch('/', {
                method: 'POST',
                body: formData,
            });

            if (!response.ok) {
                throw new Error(`服务器错误: ${response.status} ${response.statusText}`);
            }

            // --- 处理流式响应 ---
            const reader = response.body.getReader();
            const decoder = new TextDecoder('utf-8');
            responseContainer.textContent = ''; // 清空提示信息

            while (true) {
                const { done, value } = await reader.read();
                if (done) break;

                const chunk = decoder.decode(value, { stream: true });
                responseContainer.textContent += chunk;
                // 自动滚动到最底部
                responseContainer.scrollTop = responseContainer.scrollHeight;
            }

        } catch (error) {
            console.error('请求失败:', error);
            responseContainer.textContent = `客户端请求发生错误: ${error.message}`;
        } finally {
            // --- 恢复UI ---
            submitBtn.disabled = false;
            btnText.textContent = '第二步：开始分析';
            if(spinner) spinner.remove();
        }
    });
</script>

</body>
</html>

